package cloud.demarcia.crawler3.processors.Impl;

import cloud.demarcia.crawler3.entities.Page;
import cloud.demarcia.crawler3.entities.Selenarctos;
import cloud.demarcia.crawler3.entities.Skin;
import cloud.demarcia.crawler3.processors.Processor;
import cloud.demarcia.crawler3.repositories.SelenarctosRepository;
import cloud.demarcia.crawler3.repositories.SkinRepository;
import cloud.demarcia.crawler3.utils.ClassifyUtils;
import cloud.demarcia.crawler3.utils.ClockUtils;
import cloud.demarcia.crawler3.utils.StringUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import wen.Downloader;

import java.util.LinkedList;
import java.util.List;

@Service
public class SelenarctosInfoPageProcessor implements Processor {

    @Value("${init.location}")
    private String fileLocation;

    private final SelenarctosRepository selenarctosRepository;
    private final SkinRepository skinRepository;

    public SelenarctosInfoPageProcessor(SelenarctosRepository selenarctosRepository, SkinRepository skinRepository) {
        this.selenarctosRepository = selenarctosRepository;
        this.skinRepository = skinRepository;
    }

    @Override
    public List<Page> process(Document document) {

        //Initializing util classes
        List<Page> resList = new LinkedList<>();
        ClockUtils clockUtils = new ClockUtils();
        StringUtils stringUtils = new StringUtils();
        ClassifyUtils classifyUtils = new ClassifyUtils();
        Downloader skinsBigPicDownloader = new Downloader(fileLocation + "/skins/big-pics", "-big.jpg");
        Downloader skinsAvatarDownloader = new Downloader(fileLocation + "/skins/avatars", "-avatar.jpg");
        Downloader selenarctosBigPicDownloader = new Downloader(fileLocation + "/selenarctos/big-pics", "-big.jpg");

        //Extracting Skins class from documents
        List<Element> elements = document.getElementsByClass("pic-pf-list pic-pf-list3").get(0).children();
        int skinNum = elements.size();
        String bigPicLink = stringUtils.processLink4InfoPage(document.getElementsByClass("zk-con1 zk-con").get(0).attr("style"));
        int bearId = stringUtils.BearIdOnSite4InfoPage(bigPicLink);
        Selenarctos selenarctos = selenarctosRepository.findByIdOnSite(bearId);
        System.out.println("[CRAWLER] Processing " + selenarctos.getName() + "'s skins...");

        /*
         * Extracting Selenarctos information from documents
         */


        //狗熊技能信息
        List<Element> elements1 = document.getElementsByClass("skill-show").get(0).children();
        Selenarctos selenarctos1 = addSkillDesc(selenarctos, elements1);

        //狗熊职业信息
        Element element = document.getElementsByClass("herodetail-sort").get(0);
        String className = element.child(0).className();
        int selenarctosSort = stringUtils.extractSortFromDetailPage(className);
        selenarctos1.setProfession(classifyUtils.classifySelenarctosProfession(selenarctosSort));



        for (int i = 0; i < skinNum; i++) {

            Element skinElement = elements.get(i);
            Skin skin = (Skin) element2Object(skinElement);
            skin.setSelenarctosId(selenarctos.getId());

            skinRepository.save(skin);

            Element picInfo = skinElement.child(0).child(0);
            String bigLink = picInfo.attr("data-imgname");
            String avaLink = picInfo.attr("src");
            bigLink = stringUtils.urlLegalize(bigLink);
            avaLink = stringUtils.urlLegalize(avaLink);

            if (i == 0) {
                selenarctos1.setAlias(skin.getSkinName());
                selenarctosRepository.save(selenarctos1);
                selenarctosBigPicDownloader.addATask(bigLink, selenarctos.getName());
            } else {
                skinsAvatarDownloader.addATask(avaLink, skin.getSkinName());
                skinsBigPicDownloader.addATask(bigLink, skin.getSkinName());
            }
        }
        Thread download1 = new Thread(selenarctosBigPicDownloader);
        Thread download2 = new Thread(skinsAvatarDownloader);
        Thread download3 = new Thread(skinsBigPicDownloader);
        download1.start();
        download2.start();
        download3.start();
        System.out.println("[CRAWLER] Finished process " + selenarctos.getName() + ". Get " + (skinNum - 1)
                + " skins in " + clockUtils.durationString() + ".");
        return resList;
    }

    @Override
    public Object element2Object(Element skinElement) {
        Skin skin = new Skin();
        skin.setSkinName(skinElement.child(1).text());
        String tag = skinElement.child(0).child(0).attr("data-icon");
        if(tag.equals("")){
            skin.setTagNum(0);
        }else{
            skin.setTagNum(Integer.parseInt(tag));
        }
        return skin;
    }


    private Selenarctos addSkillDesc(Selenarctos selenarctos, List<Element> skills) {
        Element skill = skills.get(0);
        String[] strings = extractSkillInfo(skill);
        selenarctos.setPassiveness(strings[0]);
        selenarctos.setSkill0des(strings[1]);

        skill =skills.get(1);
        strings=extractSkillInfo(skill);
        selenarctos.setSkill1(strings[0]);
        selenarctos.setSkill1des((strings[1]));

        skill =skills.get(2);
        strings=extractSkillInfo(skill);
        selenarctos.setSkill2(strings[0]);
        selenarctos.setSkill2des((strings[1]));

        skill =skills.get(3);
        strings=extractSkillInfo(skill);
        selenarctos.setSkill3(strings[0]);
        selenarctos.setSkill3des((strings[1]));

        skill =skills.get(4);
        strings=extractSkillInfo(skill);
        selenarctos.setSkill4(strings[0]);
        selenarctos.setSkill4des((strings[1]));
        return selenarctos;
    }

    private String[] extractSkillInfo(Element element) {
        String[] strings = new String[2];
        strings[0] = element.child(0).child(0).text();
        strings[1] = element.child(1).text();
        return strings;
    }


}
